package com.linlang.grasharepro.activity.fragment;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.linlang.grasharepro.R;
import com.linlang.grasharepro.activity.MessageActivity;
import com.linlang.grasharepro.adapter.MessageAdapter;
import com.linlang.grasharepro.dao.NewMessageDB;
import com.linlang.grasharepro.model.MessageCenter;
import com.linlang.grasharepro.model.NewMessageDomain;
import com.linlang.grasharepro.model.ShareCoenten;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lixin on 2016-8-6.
 */
public class MessagePage extends Fragment {

    private View view;
    private ListView melist;
    private List<NewMessageDomain> list = new ArrayList<NewMessageDomain>();

    private NewMessageDB newMessageDB;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view =  inflater.inflate(R.layout.messagepage,container,false);
        initView();
        initData();
        return view;
    }

    private void initData() {
    }

    private void initView() {
        //获取用户登陆信息
        SharedPreferences preferences = getActivity().getSharedPreferences("userInfo",
                Activity.MODE_PRIVATE);
        String account = preferences.getString("account", "");

        melist = (ListView)view.findViewById(R.id.messagelist);
        newMessageDB = new NewMessageDB(getActivity());
        newMessageDB.open();
        list = newMessageDB.getMyNewMessage(account);

        MessageAdapter adpter = new MessageAdapter(list,getContext());
        melist.setAdapter(adpter);

        //设置单击事件
        melist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                Toast.makeText(getContext(), list.get(position).toString(), Toast.LENGTH_LONG).show();

                NewMessageDomain newMessageDomain = list.get(position);
                if(newMessageDomain.getType().equals("1")){//私信，进入私信页面
                    ShareCoenten info = new ShareCoenten();
                    info.setUserName(newMessageDomain.getFromUser());
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("shareContent", info);
                    Intent intent = new Intent(getContext(),
                            MessageActivity.class);
                    intent.putExtras(bundle);
                    startActivity(intent);
                }
            }
        });
    }
}
